﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace PIM_ATE
{
    #region PIM DATA

    class ClsDataPIMTestItem
    {
        //        CREATE TABLE PIMTestItem_MOBI (
        // [id] [int] IDENTITY(1,1) NOT NULL,
        // [ProductSN] [nvarchar](50) NOT NULL, 
        // [PIMRecordTime] [datetime] NOT NULL, 
        // [PIMFreq] [nvarchar](50) NULL,
        // [PIMName] [nvarchar](50) NOT NULL,
        // [PIMAngle] [nvarchar](50) NOT NULL,
        // [PIMValue] [float] NOT NULL,
        // [PIMPass] [nchar](10) NOT NULL
        //);
        public static List<string> getColNames()
        {
            List<string> names = new List<string>();
            names.Add("id");
            names.Add("ProductSN");
            names.Add("PIMRecordTime");
            names.Add("PIMFreq");
            names.Add("PIMName");
            names.Add("PIMAngle");
            names.Add("PIMValue");
            names.Add("PIMPass");
            return names;
        }

        public static string getColNamesUpload()
        {
            string[] names = getColNames().ToArray();
            string sColNames = "";
            for (int i = 1; i < names.Length; i++)
            {
                if (i != names.Length - 1)
                    sColNames += "[" + names[i] + "],";
                else
                    sColNames += "[" + names[i] + "]";
            }
            return sColNames;
        }
    }

    class ClsDataPIMTest
    {
        //        CREATE TABLE PIMConfig_MOBI (
        //[UserID] [nvarchar](20) NOT NULL,
        //[PIMRecordTime] [datetime] NULL,
        //[PIMShiftTime] [nvarchar](50) NULL,
        //[ProductTypeID] [nvarchar](50) NOT NULL,
        //[ProductSN] [nvarchar](50) NOT NULL,
        //[ProductTypeName] [nvarchar](50) NULL,
        //[MOID] [nvarchar](50) NOT NULL,
        //[PIM_ENAID] [nvarchar](max) NULL,
        //[PIMTestStep] [nvarchar](50) NULL, 
        //[PIMLimite] [float] NOT NULL,
        //[MaxPIMValue] [float] NOT NULL,
        //[MaxPIMValueSection][float] NOT NULL,
        //[PIMPass] [nchar](10) NOT NULL,
        //[DarkroomNum] [nchar](10) NULL
        //);
        public static List<string> getColNames()
        {
            List<string> names = new List<string>();
            names.Add("UserID");
            names.Add("PIMRecordTime");
            names.Add("PIMShiftTime");
            names.Add("ProductTypeID");
            names.Add("ProductSN");
            names.Add("ProductTypeName");
            names.Add("MOID");
            names.Add("PIM_ENAID");
            names.Add("PIMTestStep");
            names.Add("PIMLimite");
            names.Add("MaxPIMValue");
            names.Add("MaxPIMValueSection");
            names.Add("PIMPass");
            names.Add("DarkroomNum");
            names.Add("UserID2");
            return names;
        }

        public static string getColNamesUpload()
        {
            string[] names = getColNames().ToArray();
            string sColNames = "";
            for (int i = 0; i < names.Length; i++)
            {
                if (i != names.Length - 1)
                    sColNames += "[" + names[i] + "],";
                else
                    sColNames += "[" + names[i] + "]";
            }
            return sColNames;
        }
    }

    #endregion

    #region ELEC DATA

    class ClsDataELECTest
    {
        //        CREATE TABLE TTest (
        //[ProductSN] [nvarchar](50) NOT NULL,
        //[MOID] [nvarchar](50) NOT NULL,
        //[RecordTime] [datetime] NULL,
        //[ShiftTime] [nvarchar](50) NULL,
        // [ProductTypeID] [nvarchar](50) NULL,
        // [UserID] [nvarchar](20) NULL,
        // [ENAID] [nvarchar](50) NULL,
        // [TestStep] [nvarchar](50) NULL,
        // [MaxVSWR] [float] NULL,
        // [MaxISO] [float] NULL,
        // [IsPass] [nvarchar](10) NULL ,
        // [IsSwitch] [nvarchar](10) NULL
        //);

        public static List<string> getColNamesTTest()
        {
            List<string> names = new List<string>();
            names.Add("ProductSN");
            names.Add("MOID");
            names.Add("RecordTime");
            names.Add("ShiftTime");
            names.Add("ProductTypeID");
            names.Add("UserID");
            names.Add("ENAID");
            names.Add("TestStep");
            names.Add("MaxVSWR");
            names.Add("MaxISO");
            names.Add("IsPass");
            names.Add("IsSwitch");
            return names;
        }

        public static List<string> getColNamesTTestNew()
        {
            List<string> names = new List<string>();
            names.Add("ProductSN");
            names.Add("MOID");
            names.Add("RecordTime");
            names.Add("ShiftTime");
            names.Add("ProductTypeID");
            names.Add("UserID");
            names.Add("ENAID");
            names.Add("TestStep");
            names.Add("MaxVSWR");
            names.Add("MaxISO");
            names.Add("IsPass");
            names.Add("IsSwitch");

            names.Add("MaxPhase");
            names.Add("MaxAm");
            names.Add("MaxInsertloss");
            names.Add("MaxMag");
            names.Add("UserID2");
            return names;
        }
        public static List<string> getColNamesTTestNew1()
        {
            List<string> names = new List<string>();
            names.Add("ProductSN");
            names.Add("MOID");
            names.Add("RecordTime");
            names.Add("ShiftTime");
            names.Add("ProductTypeID");
            names.Add("UserID");
            names.Add("ENAID");
            names.Add("TestStep");
            names.Add("MaxVSWR");
            names.Add("MaxISO");
            names.Add("IsPass");
            names.Add("IsSwitch");

            names.Add("MaxPhase");
            names.Add("MaxAm");
            names.Add("MaxInsertloss");
            names.Add("MaxMag");
            names.Add("UserID2");
            names.Add("AllStatusId");
            return names;
        }
        public static List<string> getColNamesTTestTempNew()
        {
            List<string> names = new List<string>();
            names.Add("ProductSN");
            names.Add("MOID");
            names.Add("RecordTime");
            names.Add("ShiftTime");
            names.Add("ProductTypeID");
            names.Add("UserID");
            names.Add("ENAID");
            names.Add("TestStep");
            names.Add("MaxVSWR");
            names.Add("MaxISO");
            names.Add("IsPass");
            names.Add("IsSwitch");

            names.Add("MaxPhase");
            names.Add("MaxAm");
            names.Add("MaxInsertloss");
            names.Add("MaxMag");
            names.Add("UserID2");

            names.Add("TestType");
            names.Add("Effective");
            return names;
        }

        //        CREATE TABLE TTestItem (
        // [ProductSN] [nvarchar](50) NOT NULL,
        // [RecordTime] [datetime] NULL,
        // [TestAngle] [nvarchar](50) NOT NULL,
        // [TestItemName] nvarchar(MAX) NULL,
        // [TestTypeName] [nvarchar](50) NULL,
        // [TestBand] nvarchar(MAX) NULL,
        // [VSWR] [decimal](18, 4) NULL,
        // [ISO] [decimal](18, 4) NULL,
        // [HighSpec] [decimal](18, 4) NULL,
        // [LowSpec] [decimal](18, 4) NULL,
        // [IsPass] [nvarchar](10) NULL
        //);

        public static List<string> getColNamesTTestItem()
        {
            List<string> names = new List<string>();
            names.Add("ProductSN");
            names.Add("RecordTime");
            names.Add("TestAngle");
            names.Add("TestItemName");
            names.Add("TestTypeName");
            names.Add("TestBand");
            names.Add("VSWR");
            names.Add("ISO");
            names.Add("HighSpec");
            names.Add("LowSpec");
            names.Add("IsPass");
            return names;
        }

        public static List<string> getColNamesTTestItemNew()
        {
            List<string> names = new List<string>();
            names.Add("ProductSN");
            names.Add("RecordTime");
            names.Add("TestAngle");
            names.Add("TestItemName");
            names.Add("TestTypeName");
            names.Add("TestBand");
            names.Add("VSWR");
            names.Add("ISO");
            names.Add("HighSpec");
            names.Add("LowSpec");
            names.Add("IsPass");

            names.Add("Phase");
            names.Add("PhaseOffest");
            names.Add("Mag");
            names.Add("Am");
            names.Add("Insertloss");
            names.Add("FrePoint");
            return names;
        }
    }

    #endregion

    class ClsDataResultForAntennaBuilder
    {
        const string m_sPIMTableName = "PIMTest";
        const string m_sPIMTestItemTableName = "PIMTestItem";
        const string m_sELECTableName = "TTest";
        const string m_sELECTestItemTableName = "TTestItem";
        const string m_sELECTempTableName = "TTestTemp";
        const string m_sELECTestItemTempTableName = "TTestItemTemp";


        //const string m_sPIMTableName = "PIMTest_MOBI";
        //const string m_sPIMTestItemTableName = "PIMTestItem_MOBI";
        //const string m_sELECTableName = "TTest_MOBI";
        //const string m_sELECTestItemTableName = "TTestItem_MOBI";

        string m_sError;
        string m_sConnect;

        string m_sPIMTestStep;
        string m_sProductSN;

        public string m_sProductTypeID;
        public string m_sProductTypeName;
        public string m_sMOID;

        string m_sUser, m_sUser2;
        string m_sDarkroomNum;

        public bool isEnable = true;

        public ClsDataResultForAntennaBuilder(string alias)
        {
            m_sConnect = alias;
            IniFile.SetFileName(System.Windows.Forms.Application.StartupPath + "\\JcDevices.ini");
            isEnable = IniFile.IniReadValue("Settings", "IsUploadData", "1") != "0";
        }

        public string error()
        {
            return m_sError;
        }

        public string[] getProcodeListFromTMO()
        {
            try
            {
                List<string> lstCode = new List<string>();
                string sSql = "select distinct [ProductTypeID] from TMO order by [ProductTypeID];";
                DataTable dtProcode = MicroDBHelper.ExecuteDataTable(sSql, null, m_sConnect);
                for (int i = 0; i < dtProcode.Rows.Count; i++)
                {
                    lstCode.Add(dtProcode.Rows[i][0].ToString().ToUpper());
                }
                return lstCode.ToArray();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }
        }

        public string getProcodeByMOID(string moid)
        {
            try
            {
                string sSql = "select ProductTypeID from TMO where MOID = '" + moid + "';";
                return MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect) as string;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return "";
            }
        }

        public bool matchSN(ClsDataProduct product)
        {
            try
            {
                //            select MOID from THouse where ProductSN like 'mbtda0100324h07%';
                //            select ProductTypeID from TMO where MOID = 'MO-1702090183';
                //            select ProductTypeName from TProductType where ProductTypeID = 'TDA0100324';
                m_sProductSN = product.sProductSN.ToUpper();
                string sSql = "select MOID from THouse where ProductSN = '" + m_sProductSN + "';";
                m_sMOID = MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect) as string;
                if (m_sMOID == null || m_sMOID == "")
                {
                    m_sError = "该条码未注册，无法使用";
                    return false;
                }

                sSql = "select ProductTypeID from TMO where MOID = '" + m_sMOID + "';";
                m_sProductTypeID = MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect) as string;
                if (m_sProductTypeID == null || m_sProductTypeID == "")
                {
                    m_sError = "该条码未注册，无法使用";
                    return false;
                }

                sSql = "select ProductTypeName from TProductType where ProductTypeID = '" + m_sProductTypeID + "';";
                m_sProductTypeName = MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect) as string;
                if (m_sProductTypeName == null || m_sProductTypeName == "")
                {
                    m_sError = "该条码未注册，无法使用";
                    return false;
                }

                product.sProcode = m_sProductTypeID;
                product.sProcodeName = m_sProductTypeName;
                product.sMOID = m_sMOID;

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            return false;
        }

        public bool constructSN(string sProductSN, string sPIMTestStep, ClsDataUser users, bool bCheckPIM = false)
        {
            m_sError = "";
            m_sProductSN = sProductSN.ToUpper();
            m_sPIMTestStep = sPIMTestStep;

            m_sUser = users.sUser;
            m_sUser2 = users.sUser2;

            m_sDarkroomNum = users.sDarkroomNum;

            if (GDefine.IS_LOCAL_CONFIG_ANTENNA) return true;

            #region 检查PIMStep
            try
            {
                string sStepColName, sPassColName, sTimeColName;
                string sTableName = "";
                string sTbName = "";
                string sSql = "";
                if (!bCheckPIM)
                {
                    sStepColName = "PIMTestStep";
                    sPassColName = "PIMPass";
                    sTimeColName = "PIMRecordTime";
                    sTableName = m_sPIMTableName;
                    sTbName = m_sPIMTableName;
                }
                else
                {
                    sStepColName = "TestStep";
                    sPassColName = "Status"; //sPassColName = "IsPass";
                    sTimeColName = "Time";// sTimeColName = "RecordTime";
                    sTableName = m_sELECTableName;
                    sTbName = "tb_AllStatus";
                }

                //sSql = string.Format("select count(*) from {0} where [ProductSN] = '{1}' and [{2}] = 'PASS'",
                //                     sTableName, m_sProductSN, sPassColName);
                sSql = string.Format("select top 1 [{0}] from {1} where [ProductSN] = '{2}' order by {3} DESC",
                                    sPassColName, sTbName, m_sProductSN, sTimeColName);
                object obj = MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect);
                if (obj != null && obj.ToString().ToUpper().Contains("PASS"))
                {
                    if (System.Windows.Forms.MessageBox.Show("该条码已测试合格，是否继续？", "", System.Windows.Forms.MessageBoxButtons.OKCancel)
                        != System.Windows.Forms.DialogResult.OK)
                    {
                        m_sError = "";
                        return false;
                    }
                }

                if (m_sPIMTestStep == "成品" || m_sPIMTestStep == "半成品")
                {
                    sSql = string.Format("select count(*) from {0} where [ProductSN] = '{1}' and [{2}] = '{3}'",
                                         sTableName, m_sProductSN, sStepColName, m_sPIMTestStep);
                    int n = (int)MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect);
                    if (n > 0)
                    {
                        m_sPIMTestStep += "返修";
                        //if (m_sPIMTestStep.Contains("半成品"))
                        //    m_sPIMTestStep += "复测";
                        //else 
                        //    m_sPIMTestStep += "返修";
                    }
                }
            }
            catch (Exception ex)
            {
                m_sError = "条码数据检测错误: " + ex.Message;
                //System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
            #endregion

            if (bCheckPIM)
            {//检查互调工序
                return checkMOIDSpecial(users);
            }
            return true;

        }

        public bool checkMOIDSpecial(ClsDataUser users)
        {
            try
            {
                //int n = (int)MicroDBHelper.ExecuteScalar(
                //                             string.Format("select count(*) from {0} where [ProductSN] = '{1}' and [PIMPass] = 'PASS'",
                //                                           m_sPIMTableName, m_sProductSN), null, m_sConnect);
                string sSql = string.Format("select top 1 [PIMPass], [MaxPIMValue] from {0} where [ProductSN] = '{1}' order by [PIMRecordTime] DESC",
                                            m_sPIMTableName, m_sProductSN);
                DataTable dtSpecial = MicroDBHelper.ExecuteDataTable(sSql, null, m_sConnect);
                //object obj = MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect);
                if (dtSpecial != null && dtSpecial.Rows.Count != 0)
                {
                    string sPass = dtSpecial.Rows[0][0].ToString();
                    if (sPass.ToUpper().Contains("PASS"))
                        return true;
                }

                if (users.aSpecial.bEnable)
                {//特采
                    if (!users.aSpecial.bNeedCheckPIM)
                    {//无需 互调
                        return true;
                    }

                    if (dtSpecial != null && dtSpecial.Rows.Count != 0)
                    {
                        string sGain = dtSpecial.Rows[0][1].ToString();
                        float fGain = Convert.ToSingle(sGain);
                        if (fGain > users.aSpecial.fMinVaule && fGain <= users.aSpecial.fMaxVaule)
                        {//特采某一频段
                            return true;
                        }
                        else
                        {
                            m_sError = string.Format("互调特采不合格({0})", fGain);
                        }
                    }
                    else
                    {
                        m_sError = "互调特采不合格(未测试)";
                    }
                }
                else
                {
                    m_sError = "互调工序未完成";
                }
            }
            catch (Exception ex)
            {
                m_sError = "互调工序检测错误: " + ex.Message + "\r\n";
            }

            return false;
        }

        public bool upload(Dictionary<int, JcPIMDataMaxResult> dicResults, List<ClsDataConfigLocal> lstConfig)
        {
            if (!isEnable) return true;

            string sCells = "";
            string sSqlPIMTest = "";
            string sSqlPIMTestItem = "";
            string sColNames_pimtest = ClsDataPIMTest.getColNamesUpload();
            string sColNames_pimtestitem = ClsDataPIMTestItem.getColNamesUpload();

            //DateTime dtRecordTime = DateTime.Now;
            //string sRecordTime = dtRecordTime.ToString("yyyy-MM-dd HH:mm:ss");
            string sRecordTime = PIMFunction.getTime(m_sConnect);
            if (sRecordTime == null || sRecordTime == "")
            {
                System.Windows.Forms.MessageBox.Show("无法获取服务器时间");
                return false;
            }
            DateTime dtRecordTime = DateTime.Parse(sRecordTime);
            string sLog = string.Format("{0}, {1}, {2}\r\n", m_sProductSN, sRecordTime, m_sPIMTestStep);

            float fAllMax = float.MinValue;
            string sAllPass = "";

            for (int i = 0; i < lstConfig.Count; i++)
            {//生成测试项数据

                #region Get Result

                string sMax, sPass;
                if (dicResults.ContainsKey(i))
                {
                    sPass = dicResults[i].sPass;
                    sMax = dicResults[i].fValue.ToString();
                    if (fAllMax < dicResults[i].fValue)
                    {
                        fAllMax = dicResults[i].fValue;
                        sAllPass = sPass;
                    }
                }
                else
                {
                    continue;
                    //sMax = "0";//当测试值为0 时，不统计测试值分段
                    //sPass = GDefine.ContentFail;
                }

                if (i == lstConfig.Count - 1)
                    sLog += sMax + "\r\n";
                else
                    sLog += sMax + ", ";

                #endregion

                #region PIMTestItem
                sCells = "";
                // [ProductSN] [nvarchar](50) NOT NULL, 
                sCells += "'" + m_sProductSN + "',";
                // [PIMRecordTime] [datetime] NOT NULL, 
                sCells += "'" + sRecordTime + "',";
                // [PIMFreq] [nvarchar](50) NULL,
                sCells += "'" + lstConfig[i].BandAlias + "',";
                // [PIMName] [nvarchar](50) NOT NULL,
                sCells += "'" + lstConfig[i].RCUIndex + "',";
                // [PIMAngle] [nvarchar](50) NOT NULL,
                sCells += "'" + lstConfig[i].TestAngle + "',";
                // [PIMValue] [float] NOT NULL,
                sCells += "'" + sMax + "',";
                // [PIMPass] [nchar](10) NOT NULL
                sCells += "'" + sPass + "'";
                //sSqlPIMTestItem += "insert into PIMTestItem (" + sColNames_pimtestitem + ") values (" + sCells + ");\r\n";
                sSqlPIMTestItem += string.Format("insert into {0} ({1}) values ({2});\r\n",
                                                 m_sPIMTestItemTableName,
                                                 sColNames_pimtestitem,
                                                 sCells);
                #endregion
            }

            sLog += fAllMax + "\r\n";

            #region PIMTest
            //生成当前产品数据
            sCells = "";
            //[UserID] [nvarchar](20) NOT NULL,
            sCells += "'" + m_sUser + "',";
            //[PIMRecordTime] [datetime] NULL,
            sCells += "'" + sRecordTime + "',";
            //[PIMShiftTime] [nvarchar](50) NULL,
            sCells += "'" + PIMFunction.GetPIMShiftTime(dtRecordTime) + "',";
            //[ProductTypeID] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sProductTypeID + "',";
            //[ProductSN] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sProductSN + "',";
            //[ProductTypeName] [nvarchar](50) NULL,
            sCells += "'" + m_sProductTypeName + "',";
            //[MOID] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sMOID + "',";
            //[PIM_ENAID] [nvarchar](max) NULL,
            sCells += "'" + "NULL" + "',";
            //[PIMTestStep] [nvarchar](50) NULL, 
            sCells += "'" + m_sPIMTestStep + "',";
            //[PIMLimite] [float] NOT NULL,
            sCells += "'" + lstConfig[0].SpecReg + "',";
            //[MaxPIMValue] [float] NOT NULL,
            sCells += "'" + fAllMax.ToString() + "',";
            //[MaxPIMValueSection][float] NOT NULL,
            sCells += "'" + PIMFunction.GetValueSection(fAllMax) + "',";
            //[PIMPass] [nchar](10) NOT NULL,
            sCells += "'" + sAllPass + "',";
            //[DarkroomNum] [nchar](10) NULL
            sCells += "'" + m_sDarkroomNum + "',";
            sCells += "'" + m_sUser2 + "'";
            //sSqlPIMTest = "insert into PIMTest (" + sColNames_pimtest + ") values (" + sCells + ");";
            sSqlPIMTest = string.Format("insert into {0} ({1}) values ({2});\r\n",
                         m_sPIMTableName,
                         sColNames_pimtest,
                         sCells);

            #endregion

            Insert_Standard_ErrorLog.Insert(sLog);
            if (GDefine.IS_LOCAL_RESULT_ANTENNA) return true;

            try
            {
                using (var tran = MicroDBHelper.UseTransaction(IsolationLevel.RepeatableRead, m_sConnect))
                {
                    //执行更新
                    MicroDBHelper.ExecuteNonQuery(sSqlPIMTestItem, null, tran);
                    MicroDBHelper.ExecuteNonQuery(sSqlPIMTest, null, tran);
                    tran.MarkSuccess();

                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            return false;
        }

        public bool uploadElec(DataTable dtTestItems, Dictionary<int, JcELECDataResults> dicResults, ref bool b)
        {
            if (!isEnable) return true;
            string sCells = "";
            //DateTime dtRecordTime = DateTime.Now;
            //string sRecordTime = dtRecordTime.ToString("yyyy-MM-dd HH:mm:ss");

            string sRecordTime = "";
            if (!GDefine.IS_LOCAL_RESULT_ANTENNA)
            {
                sRecordTime = PIMFunction.getTime(m_sConnect);
                if (sRecordTime == null || sRecordTime == "")
                {
                    System.Windows.Forms.MessageBox.Show("无法获取服务器时间");
                    return false;
                }
            }
            else
            {
                sRecordTime = DateTime.Now.ToString();
            }

            DateTime dtRecordTime = DateTime.Parse(sRecordTime);
            string sColNames_test = PIMFunction.getColNameString(ClsDataELECTest.getColNamesTTest());
            string sColNames_testitem = PIMFunction.getColNameString(ClsDataELECTest.getColNamesTTestItem());

            string sSqlTTest = "";
            string sSqlTTestItem = "";
            float fAllMaxVSWR = float.MinValue;
            float fAllMaxISO = float.MaxValue;
            string sAllPass = "PASS";

            for (int i = 0; i < dtTestItems.Rows.Count; i++)
            {
                string sVSWR, sISO;
                string sPass;
                string sHighSpec, sLowSpec;

                #region spec

                if (GDefine.IS_UploadFAD && (dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString().Contains("耦合") || dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString().Contains("插损")))
                {
                    return false;
                }

                if (dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString().Contains("驻波"))
                {
                    sHighSpec = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_FactSpec].ToString().Substring(1);
                    sLowSpec = "0";
                }
                else
                {
                    sHighSpec = "0";
                    sLowSpec = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_FactSpec].ToString().Substring(1);
                }

                #endregion

                if (dicResults.ContainsKey(i))
                {
                    if (dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString().Contains("驻波"))
                    {
                        sVSWR = dicResults[i].fValue.ToString();
                        sISO = "0";

                        if (fAllMaxVSWR < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxVSWR = dicResults[i].fValue;
                        }

                    }
                    else
                    {
                        sVSWR = "0";
                        sISO = dicResults[i].fValue.ToString();

                        if (fAllMaxISO > dicResults[i].fValue)
                        {//获取最小值
                            fAllMaxISO = dicResults[i].fValue;
                        }
                    }

                    if (dicResults[i].sPass == "FAIL")
                    {
                        sAllPass = dicResults[i].sPass;
                    }

                    sPass = dicResults[i].sPass;
                }
                else
                {
                    continue;
                    //sVSWR = "0";
                    //sISO = "0";
                    //sPass = "FAIL";
                }

                string sTestItemName, sTestAngle;
                string sPortType = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortType].ToString().ToUpper();
                if (sPortType.Contains("S11"))
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_RCU].ToString();
                    sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle].ToString();
                }
                else if (sPortType.Contains("S22"))
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_RCU].ToString();
                    sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_Angle].ToString();
                }
                else
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_RCU] + "/" +
                                    dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_RCU];
                    if (dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "隔离")
                    {
                        sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle].ToString();
                    }
                    else
                    {
                        sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle] + "/" +
                                     dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_Angle];
                    }
                }

                #region TTestItem
                sCells = "";
                // [ProductSN] [nvarchar](50) NOT NULL,
                sCells += "'" + m_sProductSN + "',";
                // [RecordTime] [datetime] NULL,
                sCells += "'" + sRecordTime + "',";
                // [TestAngle] [nvarchar](50) NOT NULL,
                sCells += "'" + sTestAngle + "',";
                // [TestItemName] nvarchar(MAX) NULL,
                sCells += "'" + sTestItemName + "',";
                // [TestTypeName] [nvarchar](50) NULL,
                sCells += "'" + dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName] + "',";
                // [TestBand] nvarchar(MAX) NULL,
                sCells += "'" + dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_Freq] + "',";
                // [VSWR] [decimal](18, 4) NULL,
                sCells += "'" + sVSWR + "',";
                // [ISO] [decimal](18, 4) NULL,
                sCells += "'" + sISO + "',";
                // [HighSpec] [decimal](18, 4) NULL,
                sCells += "'" + sHighSpec + "',";
                // [LowSpec] [decimal](18, 4) NULL,
                sCells += "'" + sLowSpec + "',";
                // [IsPass] [nvarchar](10) NULL
                //sCells += "'" + dicResults[i].sPass + "'";
                sCells += "'" + sPass + "'";
                sSqlTTestItem += string.Format("insert into {0} ({1}) values ({2});\r\n",
                                                 m_sELECTestItemTableName,
                                                 sColNames_testitem,
                                                 sCells);
                #endregion
            }

            #region TTest
            //生成当前产品数据
            sCells = "";
            //[ProductSN] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sProductSN + "',";
            //[MOID] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sMOID + "',";
            //[RecordTime] [datetime] NULL,
            sCells += "'" + sRecordTime + "',";
            //[ShiftTime] [nvarchar](50) NULL,
            sCells += "'" + PIMFunction.GetPIMShiftTime(dtRecordTime) + "',";
            // [ProductTypeID] [nvarchar](50) NULL,
            sCells += "'" + m_sProductTypeID + "',";
            // [UserID] [nvarchar](20) NULL,
            sCells += "'" + m_sUser + "',";
            // [ENAID] [nvarchar](50) NULL,
            sCells += "'" + "NULL" + "',";
            // [TestStep] [nvarchar](50) NULL,
            sCells += "'" + m_sPIMTestStep + "',";
            // [MaxVSWR] [float] NULL,
            sCells += "'" + fAllMaxVSWR.ToString() + "',";
            // [MaxISO] [float] NULL,
            sCells += "'" + fAllMaxISO.ToString() + "',";
            // [IsPass] [nvarchar](10) NULL ,
            sCells += "'" + sAllPass + "',";
            // [IsSwitch] [nvarchar](10) NULL
            sCells += "'1'";
            //sSqlPIMTest = "insert into PIMTest (" + sColNames_pimtest + ") values (" + sCells + ");";
            sSqlTTest = string.Format("insert into {0} ({1}) values ({2});\r\n",
                         m_sELECTableName,
                         sColNames_test,
                         sCells);

            #endregion

            if (GDefine.IS_LOCAL_RESULT_ANTENNA) return true;

            try
            {
                using (var tran = MicroDBHelper.UseTransaction(IsolationLevel.RepeatableRead, m_sConnect))
                {
                    //执行更新
                    MicroDBHelper.ExecuteNonQuery(sSqlTTestItem, null, tran);
                    MicroDBHelper.ExecuteNonQuery(sSqlTTest, null, tran);
                    tran.MarkSuccess();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
            b = sAllPass == "PASS" ? true : false;
            return true;
        }   

        public bool uploadElecNew(DataTable dtTestItems, Dictionary<int, JcELECDataResults> dicResults, ref bool b, ref string sTime)
        {
            if (!isEnable) return true;
            if (dicResults.Count == 0) return true;
            string sCells = "";
            string sRecordTime = "";
            if (!GDefine.IS_LOCAL_RESULT_ANTENNA)
            {
                sRecordTime = PIMFunction.getTime(m_sConnect);
                if (sRecordTime == null || sRecordTime == "")
                {
                    System.Windows.Forms.MessageBox.Show("无法获取服务器时间");
                    return false;
                }
            }
            else
            {
                sRecordTime = DateTime.Now.ToString();
            }
            sTime = sRecordTime;
            DateTime dtRecordTime = DateTime.Parse(sRecordTime);
            string sColNames_test = PIMFunction.getColNameString(ClsDataELECTest.getColNamesTTestNew());
            string sColNames_testitem = PIMFunction.getColNameString(ClsDataELECTest.getColNamesTTestItemNew());

            string sSqlTTest = "";
            string sSqlTTestItem = "";
            float fAllMaxVSWR = float.MinValue;
            float fAllMaxISO = float.MaxValue;
            float fAllMaxPhaseOffest = float.MinValue;
            float fAllMaxAm = float.MinValue;
            float fAllMaxMag = float.MinValue; float fmag = float.MinValue;
            float fAllMaxInsert = float.MinValue;

            string sAllPass = "PASS";

            for (int i = 0; i < dtTestItems.Rows.Count; i++)
            {
                string sVSWR, sISO, sPhase, sPhaseOffest, sMag, sAM, sInsert;
                string sPass;
                string sHighSpec = "0", sLowSpec = "0";
                float spec0 = 0f;

                string spec = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_FactSpec].ToString();
                getSpec(spec, ref sHighSpec, ref sLowSpec, ref spec0);

                int itemAntennaType = Convert.ToInt32(dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_sAntennaType].ToString());
                string itemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString();
                if (dicResults.ContainsKey(i))
                {
                    sVSWR = sISO = sPhase = sPhaseOffest = sMag = sAM = sInsert = "0";
                    if (itemName.Contains("驻波"))
                    {
                        sVSWR = dicResults[i].fValue.ToString();
                        if (fAllMaxVSWR < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxVSWR = dicResults[i].fValue;
                        }
                    }
                    else if (itemName.Contains("隔离"))
                    {
                        sISO = dicResults[i].fValue.ToString();
                        if (fAllMaxISO > dicResults[i].fValue)
                        {//获取最小值
                            fAllMaxISO = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "相位")
                    {
                        sPhase = dicResults[i].fValue.ToString();
                    }
                    else if (itemName == "相位一致性")
                    {
                        sPhaseOffest = dicResults[i].fValue.ToString();
                        if (fAllMaxPhaseOffest < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxPhaseOffest = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "耦合")
                    {
                        sMag = dicResults[i].fValue.ToString();
                        if (itemAntennaType == (int)AntennaType.NUMAL)
                        {
                            if (fAllMaxMag < dicResults[i].fValue)
                            {//获取最大值
                                fAllMaxMag = dicResults[i].fValue;
                            }
                        }
                        else if (itemAntennaType == (int)AntennaType.TD)
                        { //耦合偏差最大值--FAD报表展示需要根据角度取出偏移最大值 ，主表不存在角度，SO 不处理
                            if (fmag < (dicResults[i].fValue - spec0))
                            {
                                fmag = (dicResults[i].fValue - spec0);
                                fAllMaxMag = dicResults[i].fValue;
                            }
                        }
                    }
                    else if (itemName == "幅度一致性")
                    {
                        sAM = dicResults[i].fValue.ToString();
                        if (fAllMaxAm < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxAm = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "插损")
                    {
                        sInsert = dicResults[i].fValue.ToString();
                        if (fAllMaxInsert < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxInsert = dicResults[i].fValue;
                        }
                    }


                    if (dicResults[i].sPass == "FAIL")
                    {
                        sAllPass = dicResults[i].sPass;
                    }
                    sPass = dicResults[i].sPass;
                }
                else
                {
                    continue;
                }

                string sTestItemName, sTestAngle;
                string sPortType = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortType].ToString().ToUpper();
                string sFrePoint = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_sMarkers].ToString().ToUpper();
                if (sPortType.Contains("S11"))
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_RCU].ToString();
                    sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle].ToString();
                }
                else if (sPortType.Contains("S22"))
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_RCU].ToString();
                    sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_Angle].ToString();
                }
                else
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_RCU] + "/" +
                                    dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_RCU];
                    if (dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "同极化隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "异极化隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "相位" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "相位一致性" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "耦合" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "幅度一致性" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "插损")
                    {
                        sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle].ToString();
                    }
                    else
                    {
                        sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle] + "/" +
                                     dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_Angle];
                    }
                }

                #region TTestItem
                sCells = "";
                // [ProductSN] [nvarchar](50) NOT NULL,
                sCells += "'" + m_sProductSN + "',";
                // [RecordTime] [datetime] NULL,
                sCells += "'" + sRecordTime + "',";
                // [TestAngle] [nvarchar](50) NOT NULL,
                sCells += "'" + sTestAngle + "',";
                // [TestItemName] nvarchar(MAX) NULL,
                sCells += "'" + sTestItemName + "',";
                // [TestTypeName] [nvarchar](50) NULL,
                sCells += "'" + dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName] + "',";
                // [TestBand] nvarchar(MAX) NULL,
                sCells += "'" + dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_Freq] + "',";
                // [VSWR] [decimal](18, 4) NULL,
                sCells += "'" + sVSWR + "',";
                // [ISO] [decimal](18, 4) NULL,
                sCells += "'" + sISO + "',";
                // [HighSpec] [decimal](18, 4) NULL,
                sCells += "'" + sHighSpec + "',";
                // [LowSpec] [decimal](18, 4) NULL,
                sCells += "'" + sLowSpec + "',";
                // [IsPass] [nvarchar](10) NULL
                //sCells += "'" + dicResults[i].sPass + "'";
                sCells += "'" + sPass + "',";

                sCells += "'" + sPhase + "',";
                sCells += "'" + sPhaseOffest + "',";
                sCells += "'" + sMag + "',";
                sCells += "'" + sAM + "',";
                sCells += "'" + sInsert + "',";
                sCells += "'" + sFrePoint + "'";

                sSqlTTestItem += string.Format("insert into {0} ({1}) values ({2});\r\n",
                                                 m_sELECTestItemTableName,
                                                 sColNames_testitem,
                                                 sCells);
                #endregion
            }

            #region TTest

            fAllMaxVSWR = fAllMaxVSWR == float.MinValue ? -1 : fAllMaxVSWR;
            fAllMaxISO = fAllMaxISO == float.MaxValue ? -1 : fAllMaxISO;
            fAllMaxPhaseOffest = fAllMaxPhaseOffest == float.MinValue ? -1 : fAllMaxPhaseOffest;
            fAllMaxAm = fAllMaxAm == float.MinValue ? -1 : fAllMaxAm;
            fAllMaxMag = fAllMaxMag == float.MinValue ? -1 : fAllMaxMag;
            fAllMaxInsert = fAllMaxInsert == float.MinValue ? -1 : fAllMaxInsert;

            //生成当前产品数据
            sCells = "";
            //[ProductSN] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sProductSN + "',";
            //[MOID] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sMOID + "',";
            //[RecordTime] [datetime] NULL,
            sCells += "'" + sRecordTime + "',";
            //[ShiftTime] [nvarchar](50) NULL,
            sCells += "'" + PIMFunction.GetPIMShiftTime(dtRecordTime) + "',";
            // [ProductTypeID] [nvarchar](50) NULL,
            sCells += "'" + m_sProductTypeID + "',";
            // [UserID] [nvarchar](20) NULL,
            sCells += "'" + m_sUser + "',";
            // [ENAID] [nvarchar](50) NULL,
            sCells += "'" + "NULL" + "',";
            // [TestStep] [nvarchar](50) NULL,
            sCells += "'" + m_sPIMTestStep + "',";
            // [MaxVSWR] [float] NULL,
            sCells += "'" + fAllMaxVSWR.ToString() + "',";
            // [MaxISO] [float] NULL,
            sCells += "'" + fAllMaxISO.ToString() + "',";
            // [IsPass] [nvarchar](10) NULL ,
            sCells += "'" + sAllPass + "',";
            // [IsSwitch] [nvarchar](10) NULL
            sCells += "'1',";

            sCells += "'" + fAllMaxPhaseOffest.ToString() + "',";
            sCells += "'" + fAllMaxAm.ToString() + "',";
            sCells += "'" + fAllMaxInsert.ToString() + "',";
            sCells += "'" + fAllMaxMag.ToString() + "',";
            sCells += "'" + m_sUser2 + "'";

            //sSqlPIMTest = "insert into PIMTest (" + sColNames_pimtest + ") values (" + sCells + ");";
            sSqlTTest = string.Format("insert into {0} ({1}) values ({2});\r\n",
                         m_sELECTableName,
                         sColNames_test,
                         sCells);

            #endregion

            if (GDefine.IS_LOCAL_RESULT_ANTENNA) return true;

            try
            {
                using (var tran = MicroDBHelper.UseTransaction(IsolationLevel.RepeatableRead, m_sConnect))
                {
                    //执行更新
                    MicroDBHelper.ExecuteNonQuery(sSqlTTestItem, null, tran);
                    MicroDBHelper.ExecuteNonQuery(sSqlTTest, null, tran);
                    tran.MarkSuccess();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
            b = sAllPass == "PASS" ? true : false;
            return true;
        }

        public bool uploadElecNew(DataTable dtTestItems, Dictionary<int, JcELECDataResults> dicResults, ref bool b, ref string sTime, int id)
        {
            if (!isEnable) return true;
            if (dicResults.Count == 0) return true;
            string sCells = "";
            string sRecordTime = "";
            sRecordTime = sTime;
            DateTime dtRecordTime = DateTime.Parse(sRecordTime);
            string sColNames_test = PIMFunction.getColNameString(ClsDataELECTest.getColNamesTTestNew1());
            string sColNames_testitem = PIMFunction.getColNameString(ClsDataELECTest.getColNamesTTestItemNew());

            string sSqlTTest = "";
            string sSqlTTestItem = "";
            float fAllMaxVSWR = float.MinValue;
            float fAllMaxISO = float.MaxValue;
            float fAllMaxPhaseOffest = float.MinValue;
            float fAllMaxAm = float.MinValue;
            float fAllMaxMag = float.MinValue; float fmag = float.MinValue;
            float fAllMaxInsert = float.MinValue;

            string sAllPass = "PASS";

            DataTable TTestItemTable = getTTestItemTable();

            for (int i = 0; i < dtTestItems.Rows.Count; i++)
            {
                string sVSWR, sISO, sPhase, sPhaseOffest, sMag, sAM, sInsert;
                string sPass;
                string sHighSpec = "0", sLowSpec = "0";
                float spec0 = 0f;

                string spec = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_FactSpec].ToString();
                getSpec(spec, ref sHighSpec, ref sLowSpec, ref spec0);

                int itemAntennaType = Convert.ToInt32(dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_sAntennaType].ToString());
                string itemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString();
                if (dicResults.ContainsKey(i))
                {
                    sVSWR = sISO = sPhase = sPhaseOffest = sMag = sAM = sInsert = "0";
                    if (itemName.Contains("驻波"))
                    {
                        sVSWR = dicResults[i].fValue.ToString();
                        if (fAllMaxVSWR < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxVSWR = dicResults[i].fValue;
                        }
                    }
                    else if (itemName.Contains("隔离"))
                    {
                        sISO = dicResults[i].fValue.ToString();
                        if (fAllMaxISO > dicResults[i].fValue)
                        {//获取最小值
                            fAllMaxISO = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "相位")
                    {
                        sPhase = dicResults[i].fValue.ToString();
                    }
                    else if (itemName == "相位一致性")
                    {
                        sPhaseOffest = dicResults[i].fValue.ToString();
                        if (fAllMaxPhaseOffest < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxPhaseOffest = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "耦合")
                    {
                        sMag = dicResults[i].fValue.ToString();
                        if (itemAntennaType == (int)AntennaType.NUMAL)
                        {
                            if (fAllMaxMag < dicResults[i].fValue)
                            {//获取最大值
                                fAllMaxMag = dicResults[i].fValue;
                            }
                        }
                        else if (itemAntennaType == (int)AntennaType.TD)
                        { //耦合偏差最大值--FAD报表展示需要根据角度取出偏移最大值 ，主表不存在角度，SO 不处理
                            if (fmag < (dicResults[i].fValue - spec0))
                            {
                                fmag = (dicResults[i].fValue - spec0);
                                fAllMaxMag = dicResults[i].fValue;
                            }
                        }
                    }
                    else if (itemName == "幅度一致性")
                    {
                        sAM = dicResults[i].fValue.ToString();
                        if (fAllMaxAm < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxAm = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "插损")
                    {
                        sInsert = dicResults[i].fValue.ToString();
                        if (fAllMaxInsert < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxInsert = dicResults[i].fValue;
                        }
                    }


                    if (dicResults[i].sPass == "FAIL")
                    {
                        sAllPass = dicResults[i].sPass;
                    }
                    sPass = dicResults[i].sPass;
                }
                else
                {
                    continue;
                }

                string sTestItemName, sTestAngle;
                string sPortType = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortType].ToString().ToUpper();
                string sFrePoint = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_sMarkers].ToString().ToUpper();
                if (sPortType.Contains("S11"))
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_RCU].ToString();
                    sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle].ToString();
                }
                else if (sPortType.Contains("S22"))
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_RCU].ToString();
                    sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_Angle].ToString();
                }
                else
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_RCU] + "/" +
                                    dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_RCU];
                    if (dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "同极化隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "异极化隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "相位" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "相位一致性" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "耦合" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "幅度一致性" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "插损")
                    {
                        sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle].ToString();
                    }
                    else
                    {
                        sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle] + "/" +
                                     dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_Angle];
                    }
                }

                #region TTestItem
                //sCells = "";
                //// [ProductSN] [nvarchar](50) NOT NULL,
                //sCells += "'" + m_sProductSN + "',";
                //// [RecordTime] [datetime] NULL,
                //sCells += "'" + sRecordTime + "',";
                //// [TestAngle] [nvarchar](50) NOT NULL,
                //sCells += "'" + sTestAngle + "',";
                //// [TestItemName] nvarchar(MAX) NULL,
                //sCells += "'" + sTestItemName + "',";
                //// [TestTypeName] [nvarchar](50) NULL,
                //sCells += "'" + dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName] + "',";
                //// [TestBand] nvarchar(MAX) NULL,
                //sCells += "'" + dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_Freq] + "',";
                //// [VSWR] [decimal](18, 4) NULL,
                //sCells += "'" + sVSWR + "',";
                //// [ISO] [decimal](18, 4) NULL,
                //sCells += "'" + sISO + "',";
                //// [HighSpec] [decimal](18, 4) NULL,
                //sCells += "'" + sHighSpec + "',";
                //// [LowSpec] [decimal](18, 4) NULL,
                //sCells += "'" + sLowSpec + "',";
                //// [IsPass] [nvarchar](10) NULL
                ////sCells += "'" + dicResults[i].sPass + "'";
                //sCells += "'" + sPass + "',";

                //sCells += "'" + sPhase + "',";
                //sCells += "'" + sPhaseOffest + "',";
                //sCells += "'" + sMag + "',";
                //sCells += "'" + sAM + "',";
                //sCells += "'" + sInsert + "',";
                //sCells += "'" + sFrePoint + "'";

                //sSqlTTestItem += string.Format("insert into {0} ({1}) values ({2});\r\n",
                //                                 m_sELECTestItemTableName,
                //                                 sColNames_testitem,
                //                                 sCells);
                #endregion

                #region TTestItem -dt
                DataRow dr = TTestItemTable.NewRow();
                dr["ProductSN"] = m_sProductSN;
                dr["RecordTime"] = sRecordTime;
                dr["TestItemName"] = sTestItemName;
                dr["TestTypeName"] = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName];
                //dr["StartF"] = null;
                //dr["StopF"] = null;
                dr["VSWR"] = sVSWR;
                dr["ISO"] = sISO;
                dr["HighSpec"] = sHighSpec;
                dr["LowSpec"] = sLowSpec;
                dr["IsPass"] = sPass;
                dr["TestAngle"] = sTestAngle;
                dr["TestBand"] = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_Freq];
                dr["Phase"] = sPhase;
                dr["PhaseOffest"] = sPhaseOffest;
                dr["Mag"] = sMag;
                dr["Am"] = sAM;
                dr["Insertloss"] = sInsert;
                dr["FrePoint"] = sFrePoint;

                TTestItemTable.Rows.Add(dr);
                #endregion
            }

            #region TTest

            fAllMaxVSWR = fAllMaxVSWR == float.MinValue ? -1 : fAllMaxVSWR;
            fAllMaxISO = fAllMaxISO == float.MaxValue ? -1 : fAllMaxISO;
            fAllMaxPhaseOffest = fAllMaxPhaseOffest == float.MinValue ? -1 : fAllMaxPhaseOffest;
            fAllMaxAm = fAllMaxAm == float.MinValue ? -1 : fAllMaxAm;
            fAllMaxMag = fAllMaxMag == float.MinValue ? -1 : fAllMaxMag;
            fAllMaxInsert = fAllMaxInsert == float.MinValue ? -1 : fAllMaxInsert;

            //生成当前产品数据
            sCells = "";
            //[ProductSN] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sProductSN + "',";
            //[MOID] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sMOID + "',";
            //[RecordTime] [datetime] NULL,
            sCells += "'" + sRecordTime + "',";
            //[ShiftTime] [nvarchar](50) NULL,
            sCells += "'" + PIMFunction.GetPIMShiftTime(dtRecordTime) + "',";
            // [ProductTypeID] [nvarchar](50) NULL,
            sCells += "'" + m_sProductTypeID + "',";
            // [UserID] [nvarchar](20) NULL,
            sCells += "'" + m_sUser + "',";
            // [ENAID] [nvarchar](50) NULL,
            sCells += "'" + "NULL" + "',";
            // [TestStep] [nvarchar](50) NULL,
            sCells += "'" + m_sPIMTestStep + "',";
            // [MaxVSWR] [float] NULL,
            sCells += "'" + fAllMaxVSWR.ToString() + "',";
            // [MaxISO] [float] NULL,
            sCells += "'" + fAllMaxISO.ToString() + "',";
            // [IsPass] [nvarchar](10) NULL ,
            sCells += "'" + sAllPass + "',";
            // [IsSwitch] [nvarchar](10) NULL
            sCells += "'1',";

            sCells += "'" + fAllMaxPhaseOffest.ToString() + "',";
            sCells += "'" + fAllMaxAm.ToString() + "',";
            sCells += "'" + fAllMaxInsert.ToString() + "',";
            sCells += "'" + fAllMaxMag.ToString() + "',";
            sCells += "'" + m_sUser2 + "',";
            sCells += "'" + id + "'";

            //sSqlPIMTest = "insert into PIMTest (" + sColNames_pimtest + ") values (" + sCells + ");";
            sSqlTTest = string.Format("insert into {0} ({1}) values ({2});\r\n",
                         m_sELECTableName,
                         sColNames_test,
                         sCells);

            #endregion

            if (GDefine.IS_LOCAL_RESULT_ANTENNA) return true;

            try
            {
                using (var tran = MicroDBHelper.UseTransaction(IsolationLevel.RepeatableRead, m_sConnect))
                {
                    //执行更新
                    //优化子表插入，数据库建立表类型，存储过程接受表类型直接插入
                    SqlParameter[] param = { new SqlParameter("@IsTemp", "0"), new SqlParameter("@TTestItemCollection", TTestItemTable) };
                    MicroDBHelper.ExecuteNonQuery("usp_TTestItem_Insert", param, tran, CommandType.StoredProcedure);

                    //MicroDBHelper.ExecuteNonQuery(sSqlTTestItem, null, tran);
                    MicroDBHelper.ExecuteNonQuery(sSqlTTest, null, tran);
                    tran.MarkSuccess();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
            b = sAllPass == "PASS" ? true : false;
            return true;
        }

        DataTable getTTestItemTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ProductSN", typeof(string));
            dt.Columns.Add("RecordTime", typeof(DateTime));
            dt.Columns.Add("TestItemName", typeof(string));
            dt.Columns.Add("TestTypeName", typeof(string));
            dt.Columns.Add("StartF", typeof(decimal));
            dt.Columns.Add("StopF", typeof(decimal));
            dt.Columns.Add("VSWR", typeof(decimal));
            dt.Columns.Add("ISO", typeof(decimal));
            dt.Columns.Add("HighSpec", typeof(decimal));
            dt.Columns.Add("LowSpec", typeof(decimal));
            dt.Columns.Add("IsPass", typeof(string));
            dt.Columns.Add("TestAngle", typeof(string));
            dt.Columns.Add("TestBand", typeof(string));
            dt.Columns.Add("Phase", typeof(decimal));
            dt.Columns.Add("PhaseOffest", typeof(decimal));
            dt.Columns.Add("Mag", typeof(decimal));
            dt.Columns.Add("Am", typeof(decimal));
            dt.Columns.Add("Insertloss", typeof(decimal));
            dt.Columns.Add("FrePoint", typeof(string));
            return dt;
        }     

        public bool uploadElecTempNew(DataTable dtTestItems, Dictionary<int, JcELECDataResults> dicResults, ref bool b)
        {
            if (!isEnable) return true;
            if (dicResults.Count == 0) return true;
            string sCells = "";
            string sRecordTime = "";
            if (!GDefine.IS_LOCAL_RESULT_ANTENNA)
            {
                sRecordTime = PIMFunction.getTime(m_sConnect);
                if (sRecordTime == null || sRecordTime == "")
                {
                    System.Windows.Forms.MessageBox.Show("无法获取服务器时间");
                    return false;
                }
            }
            else
            {
                sRecordTime = DateTime.Now.ToString();
            }

            DateTime dtRecordTime = DateTime.Parse(sRecordTime);
            string sColNames_test = PIMFunction.getColNameString(ClsDataELECTest.getColNamesTTestTempNew());
            string sColNames_testitem = PIMFunction.getColNameString(ClsDataELECTest.getColNamesTTestItemNew());

            string sSqlTTest = "";
            string sSqlTTestItem = "";
            float fAllMaxVSWR = float.MinValue;
            float fAllMaxISO = float.MaxValue;
            float fAllMaxPhaseOffest = float.MinValue;
            float fAllMaxAm = float.MinValue;
            float fAllMaxMag = float.MinValue; float fmag = float.MinValue;
            float fAllMaxInsert = float.MinValue;
            string sAllTestType = "";


            string sAllPass = "PASS";

            DataTable TTestItemTable = getTTestItemTable();

            for (int i = 0; i < dtTestItems.Rows.Count; i++)
            {
                string sVSWR, sISO, sPhase, sPhaseOffest, sMag, sAM, sInsert;
                string sPass;
                string sHighSpec = "0", sLowSpec = "0";
                float spec0 = 0f;

                string spec = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_FactSpec].ToString();
                getSpec(spec, ref sHighSpec, ref sLowSpec, ref spec0);

                int itemAntennaType = Convert.ToInt32(dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_sAntennaType].ToString());
                string itemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString();
                if (dicResults.ContainsKey(i))
                {
                    sAllTestType = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_sTestType].ToString();
                    sVSWR = sISO = sPhase = sPhaseOffest = sMag = sAM = sInsert = "0";
                    if (itemName.Contains("驻波"))
                    {
                        sVSWR = dicResults[i].fValue.ToString();
                        if (fAllMaxVSWR < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxVSWR = dicResults[i].fValue;
                        }
                    }
                    else if (itemName.Contains("隔离"))
                    {
                        sISO = dicResults[i].fValue.ToString();
                        if (fAllMaxISO > dicResults[i].fValue)
                        {//获取最小值
                            fAllMaxISO = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "相位")
                    {
                        sPhase = dicResults[i].fValue.ToString();
                    }
                    else if (itemName == "相位一致性")
                    {
                        sPhaseOffest = dicResults[i].fValue.ToString();
                        if (fAllMaxPhaseOffest < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxPhaseOffest = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "耦合")
                    {
                        sMag = dicResults[i].fValue.ToString();
                        if (itemAntennaType == (int)AntennaType.NUMAL)
                        {
                            if (fAllMaxMag < dicResults[i].fValue)
                            {//获取最大值
                                fAllMaxMag = dicResults[i].fValue;
                            }
                        }
                        else if (itemAntennaType == (int)AntennaType.TD)
                        { //耦合偏差最大值--FAD报表展示需要根据角度取出偏移最大值 ，主表不存在角度，SO 不处理
                            if (fmag < (dicResults[i].fValue - spec0))
                            {
                                fmag = (dicResults[i].fValue - spec0);
                                fAllMaxMag = dicResults[i].fValue;
                            }
                        }
                    }
                    else if (itemName == "幅度一致性")
                    {
                        sAM = dicResults[i].fValue.ToString();
                        if (fAllMaxAm < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxAm = dicResults[i].fValue;
                        }
                    }
                    else if (itemName == "插损")
                    {
                        sInsert = dicResults[i].fValue.ToString();
                        if (fAllMaxInsert < dicResults[i].fValue)
                        {//获取最大值
                            fAllMaxInsert = dicResults[i].fValue;
                        }
                    }


                    if (dicResults[i].sPass == "FAIL")
                    {
                        sAllPass = dicResults[i].sPass;
                    }
                    sPass = dicResults[i].sPass;
                }
                else
                {
                    continue;
                }

                string sTestItemName, sTestAngle;
                string sPortType = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortType].ToString().ToUpper();
                string sFrePoint = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_sMarkers].ToString().ToUpper();
                if (sPortType.Contains("S11"))
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_RCU].ToString();
                    sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle].ToString();
                }
                else if (sPortType.Contains("S22"))
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_RCU].ToString();
                    sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_Angle].ToString();
                }
                else
                {
                    sTestItemName = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_RCU] + "/" +
                                    dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_RCU];
                    if (dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "同极化隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "异极化隔离" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "相位" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "相位一致性" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "耦合" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "幅度一致性" ||
                        dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName].ToString() == "插损")
                    {
                        sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle].ToString();
                    }
                    else
                    {
                        sTestAngle = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortA_Angle] + "/" +
                                     dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_PortB_Angle];
                    }
                }

                #region TTestItem
                //sCells = "";
                //// [ProductSN] [nvarchar](50) NOT NULL,
                //sCells += "'" + m_sProductSN + "',";
                //// [RecordTime] [datetime] NULL,
                //sCells += "'" + sRecordTime + "',";
                //// [TestAngle] [nvarchar](50) NOT NULL,
                //sCells += "'" + sTestAngle + "',";
                //// [TestItemName] nvarchar(MAX) NULL,
                //sCells += "'" + sTestItemName + "',";
                //// [TestTypeName] [nvarchar](50) NULL,
                //sCells += "'" + dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName] + "',";
                //// [TestBand] nvarchar(MAX) NULL,
                //sCells += "'" + dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_Freq] + "',";
                //// [VSWR] [decimal](18, 4) NULL,
                //sCells += "'" + sVSWR + "',";
                //// [ISO] [decimal](18, 4) NULL,
                //sCells += "'" + sISO + "',";
                //// [HighSpec] [decimal](18, 4) NULL,
                //sCells += "'" + sHighSpec + "',";
                //// [LowSpec] [decimal](18, 4) NULL,
                //sCells += "'" + sLowSpec + "',";
                //// [IsPass] [nvarchar](10) NULL
                ////sCells += "'" + dicResults[i].sPass + "'";
                //sCells += "'" + sPass + "',";

                //sCells += "'" + sPhase + "',";
                //sCells += "'" + sPhaseOffest + "',";
                //sCells += "'" + sMag + "',";
                //sCells += "'" + sAM + "',";
                //sCells += "'" + sInsert + "',";
                //sCells += "'" + sFrePoint + "'";

                //sSqlTTestItem += string.Format("insert into {0} ({1}) values ({2});\r\n",
                //                                 m_sELECTestItemTempTableName,
                //                                 sColNames_testitem,
                //                                 sCells);
                #endregion

                #region TTestItem -dt
                DataRow dr = TTestItemTable.NewRow();
                dr["ProductSN"] = m_sProductSN;
                dr["RecordTime"] = sRecordTime;
                dr["TestItemName"] = sTestItemName;
                dr["TestTypeName"] = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_ItemName];
                //dr["StartF"] = null;
                //dr["StopF"] = null;
                dr["VSWR"] = sVSWR;
                dr["ISO"] = sISO;
                dr["HighSpec"] = sHighSpec;
                dr["LowSpec"] = sLowSpec;
                dr["IsPass"] = sPass;
                dr["TestAngle"] = sTestAngle;
                dr["TestBand"] = dtTestItems.Rows[i][PIM_ATE.Electrical.clsTestItem.chn_Freq];
                dr["Phase"] = sPhase;
                dr["PhaseOffest"] = sPhaseOffest;
                dr["Mag"] = sMag;
                dr["Am"] = sAM;
                dr["Insertloss"] = sInsert;
                dr["FrePoint"] = sFrePoint;

                TTestItemTable.Rows.Add(dr);
                #endregion

            }

            #region TTest

            fAllMaxVSWR = fAllMaxVSWR == float.MinValue ? -1 : fAllMaxVSWR;
            fAllMaxISO = fAllMaxISO == float.MaxValue ? -1 : fAllMaxISO;
            fAllMaxPhaseOffest = fAllMaxPhaseOffest == float.MinValue ? -1 : fAllMaxPhaseOffest;
            fAllMaxAm = fAllMaxAm == float.MinValue ? -1 : fAllMaxAm;
            fAllMaxMag = fAllMaxMag == float.MinValue ? -1 : fAllMaxMag;
            fAllMaxInsert = fAllMaxInsert == float.MinValue ? -1 : fAllMaxInsert;

            //生成当前产品数据
            sCells = "";
            //[ProductSN] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sProductSN + "',";
            //[MOID] [nvarchar](50) NOT NULL,
            sCells += "'" + m_sMOID + "',";
            //[RecordTime] [datetime] NULL,
            sCells += "'" + sRecordTime + "',";
            //[ShiftTime] [nvarchar](50) NULL,
            sCells += "'" + PIMFunction.GetPIMShiftTime(dtRecordTime) + "',";
            // [ProductTypeID] [nvarchar](50) NULL,
            sCells += "'" + m_sProductTypeID + "',";
            // [UserID] [nvarchar](20) NULL,
            sCells += "'" + m_sUser + "',";
            // [ENAID] [nvarchar](50) NULL,
            sCells += "'" + "NULL" + "',";
            // [TestStep] [nvarchar](50) NULL,
            sCells += "'" + m_sPIMTestStep + "',";
            // [MaxVSWR] [float] NULL,
            sCells += "'" + fAllMaxVSWR.ToString() + "',";
            // [MaxISO] [float] NULL,
            sCells += "'" + fAllMaxISO.ToString() + "',";
            // [IsPass] [nvarchar](10) NULL ,
            sCells += "'" + sAllPass + "',";
            // [IsSwitch] [nvarchar](10) NULL
            sCells += "'1',";

            sCells += "'" + fAllMaxPhaseOffest.ToString() + "',";
            sCells += "'" + fAllMaxAm.ToString() + "',";
            sCells += "'" + fAllMaxInsert.ToString() + "',";
            sCells += "'" + fAllMaxMag.ToString() + "',";
            sCells += "'" + m_sUser2 + "',";

            sCells += "'" + sAllTestType + "',";
            sCells += "'1'";

            //sSqlPIMTest = "insert into PIMTest (" + sColNames_pimtest + ") values (" + sCells + ");";
            sSqlTTest = string.Format("insert into {0} ({1}) values ({2});\r\n",
                         m_sELECTempTableName,
                         sColNames_test,
                         sCells);

            #endregion

            if (GDefine.IS_LOCAL_RESULT_ANTENNA) return true;

            try
            {
                using (var tran = MicroDBHelper.UseTransaction(IsolationLevel.RepeatableRead, m_sConnect))
                {
                    //执行更新
                    //插入优化
                    SqlParameter[] param = { new SqlParameter("@IsTemp", "1"), new SqlParameter("@TTestItemCollection", TTestItemTable) };
                    MicroDBHelper.ExecuteNonQuery("usp_TTestItem_Insert", param, tran, CommandType.StoredProcedure);

                    //MicroDBHelper.ExecuteNonQuery(sSqlTTestItem, null, tran);
                    MicroDBHelper.ExecuteNonQuery(sSqlTTest, null, tran);
                    tran.MarkSuccess();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
            b = sAllPass == "PASS" ? true : false;
            return true;
        }

        public bool updateElecTempNew()
        {
            if (!isEnable) return true;
            if (GDefine.IS_LOCAL_RESULT_ANTENNA) return true;
            #region TTest
            string sSqlTTest = string.Format("update {0} set [Effective]='0' where [ProductSN] ='{1}'", m_sELECTempTableName, m_sProductSN);
            #endregion
            try
            {
                using (var tran = MicroDBHelper.UseTransaction(IsolationLevel.RepeatableRead, m_sConnect))
                {
                    //执行更新
                    MicroDBHelper.ExecuteNonQuery(sSqlTTest, null, tran);
                    tran.MarkSuccess();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool downloadOtherElecTempNEW(int iTestType)
        {
            if (!isEnable) return true;
            if (GDefine.IS_LOCAL_RESULT_ANTENNA) return true;
            try
            {
                string sSqlTTest = string.Format("select top 1 * from {0} where [ProductSN] ='{1}' and [TestType]='{2}' and [Effective]='1' order by [RecordTime] desc", m_sELECTempTableName, m_sProductSN, iTestType);
                DataTable dtTest = MicroDBHelper.ExecuteDataTable(sSqlTTest, null, m_sConnect);
                if (dtTest == null || dtTest.Rows.Count == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }

        public bool uploadOtherElecTempNew(int iTestType, string recordtime)
        {
            if (!isEnable) return true;
            if (GDefine.IS_LOCAL_RESULT_ANTENNA) return true;
            try
            {
                //取临时主表
                string sSqlTTest = string.Format("select top 1 * from {0} where [ProductSN] ='{1}' and [TestType]='{2}' and [Effective]='1' order by [RecordTime] desc", m_sELECTempTableName, m_sProductSN, iTestType);
                DataTable dtTestTemp = MicroDBHelper.ExecuteDataTable(sSqlTTest, null, m_sConnect);
                if (dtTestTemp == null && dtTestTemp.Rows.Count == 0)
                {
                    return false;
                }
                string maxvswr = dtTestTemp.Rows[0]["MaxVSWR"].ToString();
                string maxiso = dtTestTemp.Rows[0]["MaxISO"].ToString();
                string ispass = dtTestTemp.Rows[0]["IsPass"].ToString().ToUpper();
                string maxph = dtTestTemp.Rows[0]["MaxPhase"].ToString();
                string maxam = dtTestTemp.Rows[0]["MaxAm"].ToString();
                string maxinsert = dtTestTemp.Rows[0]["MaxInsertloss"].ToString().ToUpper();
                string maxmag = dtTestTemp.Rows[0]["MaxMag"].ToString().ToUpper();
                // DateTime CreatDate = Convert.ToDateTime(dtTestTemp.Rows[0]["RecordTime"]);
                //只有在使用Convert.ToDateTime（或者(DateTime)转）对Object对象转化的时候，才能够保留日期的毫秒
                string time = Convert.ToDateTime(dtTestTemp.Rows[0]["RecordTime"]).ToString("yyyy-MM-dd HH:mm:ss.fff");

                //取正式主表
                sSqlTTest = string.Format("select top 1 * from {0} where [ProductSN] ='{1}' and [RecordTime]='{2}'", m_sELECTableName, m_sProductSN, recordtime);
                DataTable dtTest = MicroDBHelper.ExecuteDataTable(sSqlTTest, null, m_sConnect);
                if (dtTest == null || dtTest.Rows.Count == 0)
                {
                    return false;
                }
                string maxvswrT = dtTest.Rows[0]["MaxVSWR"].ToString();
                string maxisoT = dtTest.Rows[0]["MaxISO"].ToString();
                string ispassT = dtTest.Rows[0]["IsPass"].ToString().ToUpper();
                string maxphT = dtTest.Rows[0]["MaxPhase"].ToString();
                string maxamT = dtTest.Rows[0]["MaxAm"].ToString();
                string maxinsertT = dtTest.Rows[0]["MaxInsertloss"].ToString().ToUpper();
                string maxmagT = dtTest.Rows[0]["MaxMag"].ToString().ToUpper();

                maxvswrT = Convert.ToSingle(maxvswrT) >= Convert.ToSingle(maxvswr) ? maxvswrT : maxvswr;
                maxisoT = Convert.ToSingle(maxisoT) >= Convert.ToSingle(maxiso) ? maxisoT : maxiso;
                ispassT = (ispassT == ispass && ispassT == "PASS") ? "PASS" : "FAIL";
                maxphT = Convert.ToSingle(maxphT) >= Convert.ToSingle(maxph) ? maxphT : maxph;
                maxamT = Convert.ToSingle(maxamT) >= Convert.ToSingle(maxam) ? maxamT : maxam;
                maxinsertT = Convert.ToSingle(maxinsertT) >= Convert.ToSingle(maxinsert) ? maxinsertT : maxinsert;
                maxmagT = Convert.ToSingle(maxmagT) >= Convert.ToSingle(maxmag) ? maxmagT : maxmag;

                //更新正式主表
                sSqlTTest = string.Format("update {0} set [MaxVSWR]='{1}',[MaxISO]='{2}',[IsPass]='{3}',[MaxPhase]='{4}' ,[MaxAm]='{5}',[MaxInsertloss]='{6}',[MaxMag]='{7}' where [ProductSN] ='{8}' and [RecordTime]='{9}'", m_sELECTableName, maxvswrT, maxisoT, ispass, maxphT, maxamT, maxinsertT, maxmagT, m_sProductSN, recordtime);
                MicroDBHelper.ExecuteNonQuery(sSqlTTest, null, m_sConnect);

                //取临时副表  插入正式副表
                sSqlTTest = string.Format("select * from {0} where [ProductSN] ='{1}' and [RecordTime]='{2}'", m_sELECTestItemTempTableName, m_sProductSN, time);
                DataTable dtTestItem = MicroDBHelper.ExecuteDataTable(sSqlTTest, null, m_sConnect);
                if (dtTestItem == null && dtTestItem.Rows.Count == 0)
                {
                    return false;
                }
                foreach (DataRow row in dtTestItem.Rows)
                {
                    row["RecordTime"] = recordtime;
                }
                MicroDBHelper.SqlBulkCopyInsert(m_sELECTestItemTableName, dtTestItem, m_sConnect);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        bool getSpec(string spec, ref string sHighSpec, ref string sLowSpec, ref float spec0)
        {
            string header;
            float fac;
            float fac2;

            Regex rx = new Regex(@"(\d+)\.(\d+)|(\d+)");
            MatchCollection ms = rx.Matches(spec);
            if (ms.Count >= 2)
            {
                fac = Convert.ToSingle(ms[0].Value);
                fac2 = Convert.ToSingle(ms[1].Value);
            }
            else
            {
                fac = Convert.ToSingle(ms[0].Value);
                fac2 = 0;
            }
            spec0 = fac;

            rx = new Regex(@"(\<)|(\>)|(\=)|(\±)|(\≤)|(\≥)|(\%)");
            ms = rx.Matches(spec);
            if (ms.Count >= 2)
            {
                header = ms[0].Value + ms[1].Value;
            }
            else if (ms.Count == 1)
            {
                header = ms[0].Value;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("内控指标不合法: " + spec);
                return false;
            }

            sLowSpec = sHighSpec = "0";

            if (header == "±")
            {
                sLowSpec = (fac - fac2).ToString();
                sHighSpec = (fac + fac2).ToString();
            }
            else if (header == "%")
            {
                sLowSpec = fac.ToString();
            }
            else if (header == "<=" || header == "≤")
            {
                sHighSpec = fac.ToString();
            }
            else if (header == ">=" || header == "≥")
            {
                sLowSpec = fac.ToString();
            }
            else if (header == ">")
            {
                sLowSpec = fac.ToString();
            }
            else if (header == "<")
            {
                sHighSpec = fac.ToString();
            }
            else if (header == "=")
            {
                sLowSpec = sHighSpec = fac.ToString();
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("内控指标不合法: " + spec);
                return false;
            }
            return true;
        }
    }


    public class JcPIMDataMaxResult
    {
        public float fValue;
        public string sPass;
    }

    public class JcELECDataResults
    {
        public float fValue;
        public string sPass;
    }
}
